package com.gofaraway.service.export.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gofaraway.common.annotation.ExcelHandle;
import com.gofaraway.common.util.bean.PojoConvert;
import com.gofaraway.dao.mysql.dto.ShipmentResponseDTO;
import com.gofaraway.dao.mysql.dto.ShipmentSearchRequestDTO;
import com.gofaraway.dao.mysql.trunk.entity.StoShipment;
import com.gofaraway.dao.mysql.trunk.service.IStoShipmentService;
import com.gofaraway.service.export.service.AbstractExportExcelService;
import com.gofaraway.service.facade.request.ShipmentSearchRequest;
import com.gofaraway.service.facade.response.ShipmentResponse;
import org.apache.commons.lang3.tuple.MutablePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * @author ChenPeng
 * @Date 2024/5/14 17:57
 */
@Service
@ExcelHandle(value = "shipment")
public class ShipmentManagerServiceImpl implements AbstractExportExcelService {

    @Autowired
    private IStoShipmentService iStoShipmentService;

    @Override
    public MutablePair<Boolean, String> easyExcelExportData(Map<String, String> queryMap, FileOutputStream out) {
        String jsonString = JSON.toJSONString(queryMap);
        ShipmentSearchRequestDTO queryRequest = JSON.parseObject(jsonString, ShipmentSearchRequestDTO.class);
        queryRequest.setSize(100000);
        IPage<StoShipment> shipmentPage = iStoShipmentService.pageQuery(queryRequest);
        List<ShipmentResponse> excelDtoList = PojoConvert.beanListConvert(shipmentPage.getRecords(), ShipmentResponse::new);
        EasyExcel.write(out, ShipmentResponse.class).sheet("运输任务单").doWrite(excelDtoList);
        return MutablePair.of(true, "运输任务单");
    }
}
